<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activId']))
{
  header("location:checklogin.php");
}

{	
  if(isset($_POST['submit']))
  {
	  $name       = isset($_POST['name']) && ($_POST['name'] != "") ? $_POST['name'] : ""; 
	  $firmName   = isset($_POST['firmName']) && ($_POST['firmName'] != "") ? $_POST['firmName'] : "" ; 
	  $address    = isset($_POST['address']) && ($_POST['address'] != "") ? $_POST['address'] : "" ; 
	  $telNo      = isset($_POST['telNo']) && ($_POST['telNo'] != "") ? $_POST['telNo'] : "" ; 
	  $mobileNo   = isset($_POST['mobileNo']) && ($_POST['mobileNo'] != "") ? $_POST['mobileNo'] : "" ; 
	  $remarks    = isset($_POST['remarks']) && ($_POST['remarks'] != "") ? $_POST['remarks'] : "" ; 
  	
  	$insertParty = "INSERT INTO party (name,firmName,address,telNo,mobileNo,remarks)
  	                     VALUES ('".$name."','".$firmName."','".$address."','".$telNo."','".$mobileNo."','".$remarks."')";
    $insertPartyRes = mysql_query($insertParty);	
    $lastinsertPartyId  = mysql_insert_id();
    if(!$insertPartyRes)
    {
    	echo "Data Not Inserted".mysql_error();
    }
    else
    {
    	$loopCntDetail = 0;
    	while($loopCntDetail < count($_POST['rate']))
    	{
    		$route         = isset($_POST['route'][$loopCntDetail]) ? $_POST['route'][$loopCntDetail] : "";
    		$item          = isset($_POST['item'][$loopCntDetail]) ? $_POST['item'][$loopCntDetail] : "";
    		$rate          = isset($_POST['rate'][$loopCntDetail]) && $_POST['rate'][$loopCntDetail] !="" ? $_POST['rate'][$loopCntDetail] : 0;
    		$rateWithBill  = isset($_POST['rateWithBill'][$loopCntDetail]) && $_POST['rateWithBill'][$loopCntDetail] !="" ? $_POST['rateWithBill'][$loopCntDetail] : 0;
    		$fromId        = isset($_POST['fromId'][$loopCntDetail]) && $_POST['fromId'][$loopCntDetail] !="" ? $_POST['fromId'][$loopCntDetail] : 0;
    		$toId          = isset($_POST['toId'][$loopCntDetail]) && $_POST['toId'][$loopCntDetail] !="" ? $_POST['toId'][$loopCntDetail] : 0;
    
        if($item != "")
        {
	        $insertPartyDetail = "INSERT INTO partydetail(partyId,fromId,toId,item,rate,rateWithBill)
	                                       VALUES(".$lastinsertPartyId.",".$fromId.",".$toId.",'".$item."',".$rate.",".$rateWithBill.")";
	        $insertPartyDetailRes = mysql_query($insertPartyDetail);
	        if(!$insertPartyDetailRes)
	        {
	        	echo "data not Inserted ".mysql_error();
	        }
	        
	      }
	      $loopCntDetail++;
      }
       header("location:party.php");
    }
  }
  
  $i=0;
  $partyArray = array();
  $selectParty = "SELECT partyId,name,firmName,address,telNo,mobileNo,remarks
                    FROM party
                   ORDER BY partyId";
  $selectPartyRes = mysql_query($selectParty);
  while($partyRow = mysql_fetch_array($selectPartyRes))
  {
  	$partyArray[$i]['partyId']          = $partyRow['partyId'];
  	$partyArray[$i]['name']             = $partyRow['name'];
  	$partyArray[$i]['firmName']         = $partyRow['firmName'];
  	$partyArray[$i]['address']          = $partyRow['address'];
  	$partyArray[$i]['telNo']            = $partyRow['telNo'];
  	$partyArray[$i]['mobileNo']         = $partyRow['mobileNo'];
  	$partyArray[$i]['remarks']          = $partyRow['remarks'];
  	$i++;
  }
  
  $k=0;
  $branchArray = array();
  $selectBranch = "SELECT branchId,branchName
                     FROM branch";
  $selectBranchRes = mysql_query($selectBranch);
  while($branchRow = mysql_fetch_array($selectBranchRes))
  {
  	$branchArray['branchId'][$k]   = $branchRow['branchId'];
  	$branchArray['branchName'][$k] = $branchRow['branchName'];
  	$k++;
  }
  
 
  include("./bottom.php");
  $smarty->assign("branchArray",$branchArray);
  $smarty->assign("partyArray",$partyArray);
  $smarty->display("party.tpl");
}
?>